package com.sfy.mallchat.common.common.event.listener;

import com.sfy.mallchat.common.common.event.UserRegisterEvent;
import com.sfy.mallchat.common.user.dao.UserDao;
import com.sfy.mallchat.common.user.domain.entity.User;
import com.sfy.mallchat.common.user.domain.enums.UserActiveEnum;
import com.sfy.mallchat.common.user.service.IUserBackpackService;
import com.sfy.mallchat.common.user.service.IpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
public class UserRegisterListener {

    @Autowired
    private IUserBackpackService userBackpackService;

    @Autowired
    private UserDao userDao;

    @Autowired
    private IpService ipService;
    @Async
    @EventListener(classes = UserRegisterEvent.class)
    public void sendDB(UserRegisterEvent event) throws Throwable {
        User user = event.getUser();
        User update = new User();
        update.setId(user.getId());
        update.setLastOptTime(user.getLastOptTime());
        update.setIpInfo(user.getIpInfo());
        update.setStatus(UserActiveEnum.ONLINE.getCode());
        userDao.updateById(update);
        //用户ip详情
        ipService.refreshAsyncIpDetail(user.getId());
    }
}
